// pages/my/post/post.js
const app=getApp()
const db=wx.cloud.database().collection('Course');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    files:[],
    coach:"",
    name:'',
    price:'',
    sort:['健身','跑步','球类','瑜伽'],
    sortIndex:0,
    describeDetail:'',
    homeType:['新品','热门'],
    HTIndex:0,
    courseID:''
  },

  onLoad:function(options){
this.setData({
  courseID:options.id
})
  },


inputName: function(e){
  this.setData({
    name:e.detail.value
  })
},
inputPrice: function(e){
  this.setData({
    price:e.detail.value
  })
},
inputCoach: function(e){
  this.setData({
    coach:e.detail.value
  })
},
inputPlace:function(e){
  this.setData({
    place:e.detail.value
  })
},
chooseSort: function(e){
  this.setData({
    sortIndex: e.detail.value
  })
},
chooseHomeType: function(e){
  this.setData({
    HTIndex: e.detail.value
  })
},
inputDetailText: function(e){
  this.setData({
    describeDetail:e.detail.value
  })
  
},
  readyForSubmit: function(){
    
    
    
    if(this.data.name==""){
      wx.showToast({
        title: '未填写课程名',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if(this.data.price==""){
      wx.showToast({
        title: '未填写价格',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if(this.data.coach==""){
      wx.showToast({
        title: '未填写教练',
        icon:'none',
        duration:2000
      })
      return;
    }
    else if(this.data.describeDetail==""){
      wx.showToast({
        title: '未填写详情',
        icon:'none',
        duration:2000
      })
      return;
    }
    

    else{
      wx.showLoading({
        title:'上传中',
      })
      
     
      
      
      db.doc(courseID).update({
        data:{
          name:this.data.name,
          brief:this.data.describeDetail,
          coach:this.data.coach,
          hometype:this.data.homeType[this.data.HTIndex],
          place:this.data.place,
          price:this.data.price,
          sort:this.data.sort[this.data.sortIndex],
        },
        success:function(res){
          wx.hideLoading()
          wx.showToast({
            title: '提交成功',
          })
          wx.redirectTo({
            url: '../Class_manage/Class_manage',
          })
        },
        fail:function(res){
          wx.hideLoading()
          wx.showToast({
            title: '提交失败',
            icon:"none"
          })
        }
      })
     
    
      
    }
       
    
    
  },

  onPullDownRefresh: function () {
    wx.setNavigationBarTitle({
      title: '发布闲置',
    });
    wx.showNavigationBarLoading();
    setTimeout(function(){
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    },2000)
  }
})